package dao.impl;

import dao.ICategoryDao;
import dao.ICountryDao;
import jdbc_util.FirstApp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.Country;

import java.sql.SQLException;
import java.util.List;

public class CountryDaoImpl implements ICountryDao {
    @Override
    public void addCountry(Country country) {
        QueryRunner queryRunner=new QueryRunner();
        String sql="insert into country(countryname) values(?)";
        try {
            queryRunner.update(FirstApp.getConn(),sql,country.getCountryName());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateCountry(Country country) {
        QueryRunner queryRunner=new QueryRunner();
        String sql="update country set countryname=? where countryid=?";
        try {
            queryRunner.update(FirstApp.getConn(),sql,
                    country.getCountryName(),country.getCountryid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteCountry(int id) {
        QueryRunner queryRunner=new QueryRunner();
        String sql="delete from country where countryid=?";
        try {
            queryRunner.update(FirstApp.getConn(),sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Country> findAll() {
        List<Country> countryList=null;
        QueryRunner queryRunner=new QueryRunner();
        String sql="select * from country";
        try {
            countryList=queryRunner.query(FirstApp.getConn(),sql,new BeanListHandler<>(Country.class));
            return countryList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Country findById(int id) {
        Country country=null;
        QueryRunner queryRunner=new QueryRunner();
        String sql="select * from country where countryid=?";
        try {
            Object[] query=queryRunner.query(FirstApp.getConn(),sql,new ArrayHandler(),id);
            if(query.length>0)
            {
                country=new Country();
                country.setCountryid((Integer) query[0]);
                country.setCountryName((String) query[1]);
                return country;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Country findByCountryName(String name) {
        Country country=null;
        QueryRunner queryRunner=new QueryRunner();
        String sql="select * from country where countryname=?";
        try {
            Object[] query=queryRunner.query(FirstApp.getConn(),sql,new ArrayHandler(),name);
            if(query.length>0)
            {
                country=new Country();
                country.setCountryid((Integer) query[0]);
                country.setCountryName((String) query[1]);
                return country;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
